package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import io.rong.imkit.RongIM;

/**
 * Created by Thinkpad on 2016/3/4.
 */
public class FriendsInfo extends Activity implements View.OnClickListener {

    private HttpUtils httpUtils;
    private TextView my_name;
    public static TextView my_name11;
    private TextView my_nickname;
    private RelativeLayout setting_info;
    private TextView my_tel;
    private TextView my_company;
    private  TextView my_duty;
    private Button my_message;
    private ImageButton my_delete;
    private String id2;
    private String id;
    private  Intent intent;
    private ImageButton  friends_back12;
    private RelativeLayout give_card;
    private  FriendsInfoData friendsInfoData;
    private String mark;

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_info);
        intent = getIntent();
        id = intent.getStringExtra("Id");
        mark=intent.getStringExtra("mark");
//        Utils.showToast("ID----:"+mark);
        httpUtils = new HttpUtils();
        initData();

    }

    private void initData() {
        my_name = (TextView) findViewById(R.id.my_name);
        my_name11 = (TextView) findViewById(R.id.my_name11);
        my_nickname = (TextView) findViewById(R.id.my_nickname);
        setting_info = (RelativeLayout) findViewById(R.id.setting_info);
        my_tel = (TextView) findViewById(R.id.my_tel);
        my_company = (TextView) findViewById(R.id.my_company);
        my_duty = (TextView) findViewById(R.id.my_duty);
        give_card= (RelativeLayout)findViewById(R.id.give_card);
        friends_back12= (ImageButton)findViewById(R.id.friends_back12);


        my_message = (Button) findViewById(R.id.my_message);
        my_delete = (ImageButton) findViewById(R.id.my_delete);

        initView(id);

        give_card.setOnClickListener(this);
        friends_back12.setOnClickListener(this);
        setting_info.setOnClickListener(this);
        my_message.setOnClickListener(this);
        my_delete.setOnClickListener(this);

    }

    private void initView(String id) {

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETUSERINFO_URL1+id, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                System.out.println("这是个人信息：" + result);
                Gson gson = new Gson();
                friendsInfoData = gson.fromJson(result, FriendsInfoData.class);

                //这个是备注或者nickname
                //这里设置判断，
                if(null==mark||"".equals(mark)||"-".equals(mark)){

                    my_name11.setText(friendsInfoData.getPhone());

                }else {
                    my_name11.setText(mark);

                }

                my_name.setText(friendsInfoData.getRealname()); ;//真实姓名
                my_nickname.setText(friendsInfoData.getNickname());
                String name=null;
                if(friendsInfoData.getPhone().length()>3){
                    name=friendsInfoData.getPhone().substring(0,3)+"...";
                }else{
                    name=friendsInfoData.getPhone();
                }
                my_tel.setText(name);
                my_company.setText(friendsInfoData.getCompany());
                my_duty .setText(friendsInfoData.getDuty());

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.friends_back12 :
                //关闭
                finish();
                break;
            case R.id.setting_info :

                //打开个人信息窗口，然后点击下面的发送按钮的话会跳转到发送消息页面
                Intent intent = new Intent(FriendsInfo.this,FriendsSetting.class);
                //吧点击条目的id获取到传递进下一个页面。
                intent.putExtra("Id1", id);

                startActivity(intent);

                break;

            case R.id.my_message :
                if(null!=id){
                    //发送消息
                    if (RongIM.getInstance() != null) {
                        RongIM.getInstance().startPrivateChat(FriendsInfo.this, id, "hello");
                    }

                }

                break;

            case R.id.give_card :
                //发送消息
                //打开个人信息窗口，然后点击下面的发送按钮的话会跳转到发送消息页面
                Intent intent1 = new Intent(FriendsInfo.this,MyTicketActivity1.class);
                //吧点击条目的id获取到传递进下一个页面。
                intent1.putExtra("frId", friendsInfoData.getID());
                Utils.showToast("frId" + friendsInfoData.getID());
                startActivity(intent1);
                break;

            case R.id.my_delete :
                //删除好友  userID&friendsID=friendsID

                httpUtils.send(HttpRequest.HttpMethod.GET, Constans.DELETEFRIENDS_URL+14+"&friendsID="+id, new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {

                        String result = responseInfo.result;
                        System.out.println("这是个人信息：" + result);
                        Utils.showToast("删除成功");
                        finish();

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Utils.showToast("删除失败，请检查网络");
                        finish();
                    }
                });

                break;
        }

    }
}
